<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
      <el-form-item label="考试编号" prop="examCode">
        <el-input v-model="dataForm.examCode" placeholder="考试编号"></el-input>
      </el-form-item>
      <el-form-item label="考试名称" prop="examName">
        <el-input v-model="dataForm.examName" placeholder="考试名称"></el-input>
      </el-form-item>
      <el-form-item label="考试时间" prop="examTime">
      <div class="block">
          <el-date-picker
            v-model="dataForm.examTime"
            type="datetime"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </div></el-form-item>
      <el-form-item label="考试学生编码" prop="examStudentCode">
        <el-input
          v-model="dataForm.examStudentCode"
          placeholder="考试学生编码"
        ></el-input>
      </el-form-item>
      <el-form-item label="考试学生姓名" prop="examStudentName">
        <el-input
          v-model="dataForm.examStudentName"
          placeholder="考试学生姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="改卷老师编号" prop="examTeacherCode">
        <el-input
          v-model="dataForm.examTeacherCode"
          placeholder="改卷老师编号"
        ></el-input>
      </el-form-item>
      <el-form-item label="改卷老师姓名" prop="examTeacherName">
        <el-input
          v-model="dataForm.examTeacherName"
          placeholder="改卷老师姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="考试内容" prop="examContent">
      <el-input type="textarea"
          :rows="5"
          placeholder="请输入内容" v-model="dataForm.examContent"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        gmtCreate: "",
        createBy: "",
        gmtModified: "",
        lastModifiedBy: "",
        isDeleted: "",
        sortNo: "",
        version: "",
        examCode: "",
        examName: "",
        examTime: "",
        examType: "",
        examContent: "",
        examStudentCode: "",
        examStudentName: "",
        examGrade: "",
        examTeacherCode: "",
        examTeacherName: "",
        examStatus: "",
        examSubmitStatus: "",
      },
      dataRule: {
        gmtCreate: [
          { required: true, message: "记录创建时间不能为空", trigger: "blur" },
        ],
        createBy: [
          { required: true, message: "记录创建者不能为空", trigger: "blur" },
        ],
        gmtModified: [
          { required: true, message: "记录更新时间不能为空", trigger: "blur" },
        ],
        lastModifiedBy: [
          {
            required: true,
            message: "记录最后更新人不能为空",
            trigger: "blur",
          },
        ],
        isDeleted: [
          {
            required: true,
            message: "是否删除(1删除,0未删除)不能为空",
            trigger: "blur",
          },
        ],
        sortNo: [{ required: true, message: "序号不能为空", trigger: "blur" }],
        version: [{ required: true, message: "版本不能为空", trigger: "blur" }],
        examCode: [
          { required: true, message: "考试编号不能为空", trigger: "blur" },
        ],
        examName: [
          { required: true, message: "考试名称不能为空", trigger: "blur" },
        ],
        examTime: [
          { required: true, message: "考试时间不能为空", trigger: "blur" },
        ],
        examType: [
          { required: true, message: "考试科目不能为空", trigger: "blur" },
        ],
        examContent: [
          { required: true, message: "考试内容不能为空", trigger: "blur" },
        ],
        examStudentCode: [
          { required: true, message: "考试学生编码不能为空", trigger: "blur" },
        ],
        examStudentName: [
          { required: true, message: "考试学生姓名不能为空", trigger: "blur" },
        ],
        examGrade: [
          { required: true, message: "考试成绩不能为空", trigger: "blur" },
        ],
        examTeacherCode: [
          { required: true, message: "改卷老师编号不能为空", trigger: "blur" },
        ],
        examTeacherName: [
          { required: true, message: "改卷老师姓名不能为空", trigger: "blur" },
        ],
        examStatus: [
          {
            required: true,
            message: "考试状态(0_未考试,1_考试中,2_考试完成,3_已过期)不能为空",
            trigger: "blur",
          },
        ],
        examSubmitStatus: [
          {
            required: true,
            message: "考试提交状态(0_待提交,2_提交改卷中,3_已提交改卷)不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/exam/texaminformation/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.gmtCreate = data.tExamInformation.gmtCreate;
              this.dataForm.createBy = data.tExamInformation.createBy;
              this.dataForm.gmtModified = data.tExamInformation.gmtModified;
              this.dataForm.lastModifiedBy =
                data.tExamInformation.lastModifiedBy;
              this.dataForm.isDeleted = data.tExamInformation.isDeleted;
              this.dataForm.sortNo = data.tExamInformation.sortNo;
              this.dataForm.version = data.tExamInformation.version;
              this.dataForm.examCode = data.tExamInformation.examCode;
              this.dataForm.examName = data.tExamInformation.examName;
              this.dataForm.examTime = data.tExamInformation.examTime;
              this.dataForm.examType = data.tExamInformation.examType;
              this.dataForm.examContent = data.tExamInformation.examContent;
              this.dataForm.examStudentCode =
                data.tExamInformation.examStudentCode;
              this.dataForm.examStudentName =
                data.tExamInformation.examStudentName;
              this.dataForm.examGrade = data.tExamInformation.examGrade;
              this.dataForm.examTeacherCode =
                data.tExamInformation.examTeacherCode;
              this.dataForm.examTeacherName =
                data.tExamInformation.examTeacherName;
              this.dataForm.examStatus = data.tExamInformation.examStatus;
              this.dataForm.examSubmitStatus =
                data.tExamInformation.examSubmitStatus;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/exam/texaminformation/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              gmtCreate: this.dataForm.gmtCreate,
              createBy: this.dataForm.createBy,
              gmtModified: this.dataForm.gmtModified,
              lastModifiedBy: this.dataForm.lastModifiedBy,
              isDeleted: this.dataForm.isDeleted,
              sortNo: this.dataForm.sortNo,
              version: this.dataForm.version,
              examCode: this.dataForm.examCode,
              examName: this.dataForm.examName,
              examTime: this.dataForm.examTime,
              examType: this.dataForm.examType,
              examContent: this.dataForm.examContent,
              examStudentCode: this.dataForm.examStudentCode,
              examStudentName: this.dataForm.examStudentName,
              examGrade: this.dataForm.examGrade,
              examTeacherCode: this.dataForm.examTeacherCode,
              examTeacherName: this.dataForm.examTeacherName,
              examStatus: this.dataForm.examStatus,
              examSubmitStatus: this.dataForm.examSubmitStatus,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
